<?php
namespace application\admin\controllers;

use mz\ars\Contact;
use Yii;
use mz\controllers\AdminController as Controller;

class ContactController extends Controller
{

    #联系我们
    public function actionIndex()
    {
        $contact = Contact::find()->asArray()->one();
        return $this->render('index',compact('contact'));
    }

    # 执行保存的操作
    public function actionSave()
    {
        $post = Yii::$app->request->post();

        if (empty($post))return $this->ajaxReturn(['e' => 1,'m' => '提交数据不能为空']);

        $contactObj = !empty($post['id']) ? Contact::find()->where(['id' => $post['id']])->one() : new Contact();
        array_filter($post,function($key){
            return in_array($key,['wechat','qq','address','tel']);
        },ARRAY_FILTER_USE_KEY);
        $contactObj->attributes = $post;
        if ($contactObj->validate()){
            if ($contactObj->save())return $this->ajaxReturn(['e' => 0 , 'm' => '数据保存成功！']);
        } else {
            return $this->ajaxReturn(['e' => 0 , 'm' => '数据验证失败！']);
        }

        return $this->ajaxReturn(['e' => 1, 'm' => '很遗憾，数据保存失败~']);
    }
}